﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using HtmlAgilityPack;
using MvcHtml.Models;
using System.Diagnostics;

namespace MvcHtml.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "你的应用程序说明页。";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "你的联系方式页。";

            return View();
        }

        object o = new object();
        public List<Result> list1 = new List<Result>();
        public List<Result> list2 = new List<Result>();

        public ActionResult Result()
        {
            ViewBag.Message = "采集博客园数据";
            var t1 = GetThreadResult1();
            var t2 = GetThreadResult2();
            return View(t1);
        }

        public List<Result> GetThreadResult1()
        {
            string[] strs = new string[3] { "http://www.cnblogs.com/?1", "http://www.cnblogs.com/?2", "http://www.cnblogs.com/?3" };
            Stopwatch watch = new Stopwatch();
            watch.Start();
            List<Thread> threadList = new List<Thread>();
            for (int i = 0; i < strs.Length;i++ )
            {
                Int32 temp = i;
                Thread thread = new Thread(() => GetResult1(strs[temp]));
                thread.Start();
                threadList.Add(thread);
            }
            foreach (var item in threadList)
            {
                item.Join();
            }
            watch.Stop();
            ViewBag.time1 = watch.ElapsedMilliseconds;
            ViewBag.total1 = list1.Count;
            ViewBag.list1 = list1;
            return list1;
        }

        public List<Result> GetThreadResult2()
        {
            string[] strs = new string[3] { "http://www.cnblogs.com/?1", "http://www.cnblogs.com/?2", "http://www.cnblogs.com/?3" };
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < strs.Length; i++)
            {
                list2.AddRange(GetResult2(strs[i]));
            }
            watch.Stop();
            ViewBag.time2 = watch.ElapsedMilliseconds;
            ViewBag.total2 = list2.Count;
            return list2;
        }

        public void GetResult1(string str)
        {
            HtmlWeb htmlWeb = new HtmlWeb();
            HtmlDocument htmlDoc = htmlWeb.Load(str);

            //选择博客园首页文章列表
            htmlDoc.DocumentNode.SelectNodes("//div[@id='post_list']/div[@class='post_item']").
                AsParallel().ToList().ForEach(ac =>
                {
                    HtmlNode node = ac.SelectSingleNode(".//p[@class='post_item_summary']/a/img");
                    var imgDefault = "http://pic.cnitblog.com/face/u450901.jpg?id=19120257";
                    list1.Add(new Result
                    {
                        url = ac.SelectSingleNode(".//a[@class='titlelnk']").Attributes["href"].Value,
                        title = ac.SelectSingleNode(".//a[@class='titlelnk']").InnerText,
                        img = node == null ? imgDefault : node.Attributes["src"].Value.Contains("avatar.png") ? imgDefault : node.Attributes["src"].Value,
                        content = ac.SelectSingleNode(".//p[@class='post_item_summary']").InnerText
                    }); 
                });
        }

        public List<Result> GetResult2(string str)
        {
            List<Result> list = new List<Models.Result>();
            HtmlWeb htmlWeb = new HtmlWeb();
            HtmlDocument htmlDoc = htmlWeb.Load(str);

            //选择博客园首页文章列表
            htmlDoc.DocumentNode.SelectNodes("//div[@id='post_list']/div[@class='post_item']").
                AsParallel().ToList().ForEach(ac =>
                {
                    HtmlNode node = ac.SelectSingleNode(".//p[@class='post_item_summary']/a/img");
                    var imgDefault = "http://pic.cnitblog.com/face/u450901.jpg?id=19120257";
                    list.Add(new Result
                    {
                        url = ac.SelectSingleNode(".//a[@class='titlelnk']").Attributes["href"].Value,
                        title = ac.SelectSingleNode(".//a[@class='titlelnk']").InnerText,
                        img = node == null ? imgDefault : node.Attributes["src"].Value.Contains("avatar.png") ? imgDefault : node.Attributes["src"].Value,
                        content = ac.SelectSingleNode(".//p[@class='post_item_summary']").InnerText
                    });
                });
            return list;
        }
    }
}
